package springboot.test15.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import springboot.test15.bean.Student;
import springboot.test15.bean.User;
import springboot.test15.sevice.StudentService;

import java.util.Arrays;
import java.util.List;


@Controller
public class tableController {
    @GetMapping("/basic_table")
    public String basic_table(){
        return "DataTables/basic_table";
    }

    @Autowired
    StudentService studentService;
    @GetMapping("/dynamic_table")
    public String dynamic_table(@RequestParam(value = "page",defaultValue = "1")Integer page, Model model){
/*        //遍历表格内容
        List<User> users= Arrays.asList(new User("Dzy","123"), new User("Dzc","456"));
        model.addAttribute("users",users);*/

        //分页查询数据
        Page<Student> Pages = new Page<>(page,5);
        //分页查询结果
        Page<Student> studentPage = studentService.page(Pages, null);
        model.addAttribute("page",studentPage);

        List<Student> list = studentService.list();
        //model.addAttribute("students",list);
        return "DataTables/dynamic_table";
    }
    @GetMapping("/student/delete/{id}")
    public String deleteStudent(@PathVariable("id") Long id,
                                @RequestParam(value="page",defaultValue = "1")Integer page,
                                RedirectAttributes redirectAttributes){
        studentService.removeById(id);
        redirectAttributes.addAttribute("page",page);
        return "redirect:/dynamic_table";
    }
    @GetMapping("/responsive_table")
    public String responsive_table(){
        return "DataTables/responsive_table";
    }
    @GetMapping("/editable_table")
    public String editable_table(){
        return "DataTables/editable_table";
    }
}
